//
//  OrderMessageViewController.m
//  ShangAPP
//
//  Created by 徐番茄丶 on 16/1/23.
//  Copyright © 2016年 wangdao. All rights reserved.
//

#import "OrderMessageViewController.h"
#import "MyTools.h"
#import "StateOrderTableViewCell.h"
#import "MessageOrderTableViewCell.h"
@interface OrderMessageViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    UIButton               * _orderMessagebtn;
    UIButton               * _orderStatebtn;
    UIView                 * _lineview;
    
    UITableView            * _messageTab;
    UITableView            * _stateTab;
    
    NSMutableArray         * _stateImageArr;//订单状态的图片
    UIPanGestureRecognizer * _myPan;//手势
    NSMutableArray         * _stateArr;//状态的个数
    NSMutableDictionary    * _alldic;//订单的所有信息
    NSArray                * _infoarr;//菜品信息
    
    NSMutableArray         * _dataArray1;//订单详情cell内容 右..
    NSMutableArray         * _dataArray2;//订单详情cell内容 左..
    
}

@end

@implementation OrderMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"订单详情";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    
    [self getDate];
}

-(void)getDate{
    NSString *url = @"http://www.xsy777.com/app/demo/memberorderinfo.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{@"id":_idStr,
                           @"user_token":[user objectForKey:@"token"]};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:url parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            
            _alldic = [[NSMutableDictionary alloc]initWithDictionary:[responseObject objectForKey:@"data"]];
            
            if (![[[responseObject objectForKey:@"data"] objectForKey:@"info"] isEqual:[NSNull null]]) {
                _infoarr = [[NSArray alloc]initWithArray:[[responseObject objectForKey:@"data"] objectForKey:@"info"]];
            }
            NSMutableArray *arr = [[NSMutableArray alloc]initWithArray:[[[responseObject objectForKey:@"data"] objectForKey:@"pseudo"] objectForKey:@"order_status"]];
            
            _stateArr = [[NSMutableArray alloc]init];
            //_stateImageArr = [[NSMutableArray alloc]initWithObjects:@"订单已提交",@"支付成功",@"商家已接单",@"配送员取货中",@"已申请退款", nil];
            
            for (NSDictionary *dic in arr) {
                NSMutableDictionary *dic1 = [[NSMutableDictionary alloc]init];
                [dic1 setObject:[dic objectForKey:@"status"] forKey:@"status"];
                [dic1 setObject:[dic objectForKey:@"time"] forKey:@"time"];
                [dic1 setObject:[dic objectForKey:@"content"] forKey:@"content"];
                
                int stauts = [[dic objectForKey:@"status"] intValue];
                
                if (stauts == 0) {
                    [dic1 setObject:@"未支付" forKey:@"image"];
                    [dic1 setObject:@"未支付" forKey:@"status_title"];
                }else if (stauts == 1) {
                    [dic1 setObject:@"已完成1" forKey:@"image"];
                    [dic1 setObject:@"支付成功" forKey:@"status_title"];
                }else if (stauts == 2) {
                    [dic1 setObject:[[dic objectForKey:@"bid"] objectForKey:@"mobile"] forKey:@"mobile"];
                    [dic1 setObject:@"商家已接单" forKey:@"status_title"];
                    [dic1 setObject:@"已审核" forKey:@"image"];
                }else if (stauts == 9){
                    [dic1 setObject:@"商家制作完成" forKey:@"status_title"];
                    [dic1 setObject:@"已完成1" forKey:@"image"];
                }else if (stauts == 8){
                    [dic1 setObject:@"取消退款" forKey:@"status_title"];
                    [dic1 setObject:@"取消" forKey:@"image"];
                }else if (stauts == 3){
                    [dic1 setObject:@"派送员取货中" forKey:@"status_title"];
                    [dic1 setObject:@"取货中" forKey:@"image"];
                }else if (stauts == 4){
                    [dic1 setObject:[[dic objectForKey:@"pai_uid"] objectForKey:@"mobile"] forKey:@"mobile"];
                    [dic1 setObject:@"派送员配送中" forKey:@"status_title"];
                    [dic1 setObject:@"配送中1" forKey:@"image"];
                }else if (stauts == 5 || stauts == 6){
                    [dic1 setObject:@"已完成" forKey:@"status_title"];
                    [dic1 setObject:@"已完成1" forKey:@"image"];
                }else if (stauts == -3){
                    [dic1 setObject:@"取消订单" forKey:@"status_title"];
                    [dic1 setObject:@"取消" forKey:@"image"];
                }else if (stauts == -2){
                    [dic1 setObject:@"用户已删除" forKey:@"status_title"];
                    [dic1 setObject:@"管理员删除" forKey:@"image"];
                }else if (stauts == -1){
                    [dic1 setObject:@"管理员删除" forKey:@"status_title"];
                    [dic1 setObject:@"管理员删除" forKey:@"image"];
                }else if (stauts == 7){
                    [dic1 setObject:@"重新分配订单" forKey:@"status_title"];
                    [dic1 setObject:@"已评论" forKey:@"image"];
                }
                
                [_stateArr addObject:dic1];//NSLog(@"%lu",(unsigned long)_stateArr.count);
            }
            
            NSLog(@"订单状态%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"status"]);
            
#pragma mark - 活动优惠是否显示的数据源  以及判断
//            [[_alldic objectForKey:@"pseudo"] objectForKey:@"firstprice"]
            
            NSArray * arrarr =@[[[_alldic objectForKey:@"pseudo"] objectForKey:@"firstprice"],
                                [[_alldic objectForKey:@"pseudo"] objectForKey:@"money"],
                                [[_alldic objectForKey:@"pseudo"] objectForKey:@"score2_price"],
                                [[_alldic objectForKey:@"pseudo"] objectForKey:@"coupon_price"]
                                ];
            _dataArray1 =[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3", nil];
            [_dataArray1 addObjectsFromArray:arrarr];
            _dataArray2 =[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"首单立减",@"活动立减",@"积分所抵金额",@"优惠金额",nil];
            
            NSLog(@"122222222222222221111%@",_dataArray2);
//去掉为0的
//            for (int i=0; i< _dataArray2.count; i++) {
//                NSString * str =[NSString stringWithFormat:@"%@",_dataArray1[i]];
//                NSLog(@"%@",str);
//                if ([str isEqualToString:@"0.00"] ) {
//                    [_dataArray1 removeObjectAtIndex:i];
//                    [_dataArray2 removeObjectAtIndex:i];
//                }
//            }
            //数组1去掉
            for (NSString * str in _dataArray1) {
                if ([str isEqualToString:@"0.00"]){
//                    NSUInteger indexx =[_dataArray1 indexOfObject:str];
//                    NSLog(@"%ld",indexx);
                    [_dataArray1 removeObject: str];
//                    [_dataArray2 removeObjectAtIndex:indexx];
                    break;
                }
            }
            //数组2去掉
            if ([[[_alldic objectForKey:@"pseudo"] objectForKey:@"firstprice"]isEqualToString:@"0.00"]) {
                [_dataArray2 removeObject:@"首单立减"];
            }
            if ([[[_alldic objectForKey:@"pseudo"] objectForKey:@"money"]isEqualToString:@"0.00"]){
                [_dataArray2 removeObject:@"活动立减"];
            }
            if ([[[_alldic objectForKey:@"pseudo"] objectForKey:@"score2_price"]isEqualToString:@"0.00"]){
                [_dataArray2 removeObject:@"积分所抵金额"];
            }
            if ([[[_alldic objectForKey:@"pseudo"] objectForKey:@"coupon_price"]isEqualToString:@"0.00"]){
                [_dataArray2 removeObject:@"优惠金额"];
            }
            
//判断是否为首单
//            if ([[[_alldic objectForKey:@"pseudo"] allKeys] containsObject:@"firstprice"]) {
//                [_dataArray1 addObject:[[_alldic objectForKey:@"pseudo"] objectForKey:@"firstprice"]];
//                [_dataArray2 addObject:@"首单立减"];
//            }
            
            NSLog(@"12222222222222%@",_dataArray2);
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self viewCreate];
            
        }else{
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
        
    }];
    
}

//两个btn
-(void)viewCreate{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 50 * MYHEIGHT, WIDTH, 1)];
    view.backgroundColor = UIColorFromRGB(0xcecece);
    [self.view addSubview:view];
    
    _myPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    
    [self.view addGestureRecognizer:_myPan];
    
    
    _orderStatebtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDTH / 2, 50 * MYHEIGHT)];
    _orderStatebtn.backgroundColor = [UIColor whiteColor];
    _orderStatebtn.userInteractionEnabled = NO;
    _orderStatebtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_orderStatebtn setTitle:@"订单详情" forState:UIControlStateNormal];
    [_orderStatebtn setTitleColor:UIColorFromRGB(MYColor) forState:UIControlStateNormal];
    [_orderStatebtn addTarget:self action:@selector(orderState:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_orderStatebtn];
    
    _orderMessagebtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH / 2 , 0, WIDTH / 2, 50 * MYHEIGHT)];
    _orderMessagebtn.backgroundColor = [UIColor whiteColor];
    _orderMessagebtn.userInteractionEnabled = YES;
    _orderMessagebtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_orderMessagebtn setTitle:@"订单状态" forState:UIControlStateNormal];
    [_orderMessagebtn addTarget:self action:@selector(orderMessage:) forControlEvents:UIControlEventTouchUpInside];
    [_orderMessagebtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
    [self.view addSubview:_orderMessagebtn];
    
    
    _lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 49 * MYHEIGHT, WIDTH/2, 2)];
    _lineview.backgroundColor = UIColorFromRGB(MYColor);
    [self.view addSubview:_lineview];
    
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(WIDTH / 2 - 0.5, 15 * MYHEIGHT, 1, 20 * MYHEIGHT)];
    view1.backgroundColor = UIColorFromRGB(0xcecece);
    [self.view addSubview:view1];
    
    [self createStateTab];
    [self createMessageTab];
}

//状态
-(void)createStateTab{
    
    _stateTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 50 * MYHEIGHT, WIDTH, HEIGHT - 120 * MYHEIGHT)];
    _stateTab.tag = 2;
    _stateTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _stateTab.delegate = self;
    _stateTab.dataSource = self;
//    _stateTab.showsVerticalScrollIndicator = NO;
    _stateTab.backgroundColor = UIColorFromRGB(0xefeff1);
    [self.view addSubview:_stateTab];
    
}

//详情
-(void)createMessageTab{
    _messageTab = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH, 50 * MYHEIGHT, WIDTH, HEIGHT - 120 * MYHEIGHT)];
    _messageTab.tag = 1;
    _messageTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _messageTab.delegate = self;
    _messageTab.dataSource = self;
    _messageTab.showsVerticalScrollIndicator = NO;
    _messageTab.backgroundColor = UIColorFromRGB(0xefeff1);
    [self.view addSubview:_messageTab];
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 0, WIDTH, 50 * MYHEIGHT)];
    view2.backgroundColor = UIColorFromRGB(0xefeff1);
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 35 * MYHEIGHT)];
    lab.backgroundColor = UIColorFromRGB(MYColor);
    lab.textColor = UIColorFromRGB(0x333333);
    lab.text = [NSString stringWithFormat:@"%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"create_time"]];
    lab.textAlignment = NSTextAlignmentCenter;
    lab.font = [UIFont systemFontOfSize:15];
    lab.layer.masksToBounds = YES;
    lab.layer.cornerRadius = 5;
    [view2 addSubview:lab];
    _messageTab.tableHeaderView = view2;
    
}

#pragma mark tableview代理

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView.tag == 1) {
        return 1;
    }else if (tableView.tag == 2){
        return 4;
    }
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //设置每组有多少行
    if (tableView.tag == 1) {
        return _stateArr.count;
    }else if (tableView.tag == 2){
        if (section == 0) {
            return _dataArray1.count;
        }else if (section == 1){
            return 1;
        }else if (section == 2){
            return 3;
        }else if (section == 3){
            return 3;
        }
    }
    
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 1) {
        return 100;
    }else if (tableView.tag == 2){
        if (indexPath.section == 0) {
            if (indexPath.row == 1) {
                return 40;
            }
            if (indexPath.row > 0 && indexPath.row <= _infoarr.count) {
                return 70;
            }
        }else if (indexPath.section == 1){
            
            return 50;
        }else if (indexPath.section == 2){
            if (indexPath.row == 1) {
                return 85;
            }
        }
        return 50;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //static NSString *identifier = @"cell";
    if (tableView.tag == 1) {
        static NSString *identifier = @"cell";
        StateOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[StateOrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        NSDictionary *dic = [_stateArr objectAtIndex:indexPath.row];
        cell.titlelab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"status_title"]];
        cell.timelab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"time"]];
        if ([[dic objectForKey:@"status"] intValue] == 2) {
            cell.messagelab.text = [NSString stringWithFormat:@"商家电话：%@",[dic objectForKey:@"mobile"]];
            [cell.phonebtn setBackgroundImage:[UIImage imageNamed:@"phone1"] forState:UIControlStateNormal];
        }else if ([[dic objectForKey:@"status"] intValue] == 4){
            cell.messagelab.text = [NSString stringWithFormat:@"派送员电话：%@",[dic objectForKey:@"mobile"]];
            [cell.phonebtn setBackgroundImage:[UIImage imageNamed:@"phone1"] forState:UIControlStateNormal];
        }else{
            cell.messagelab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"content"]];
        }
        
        cell.myImage.image = [UIImage imageNamed:[dic objectForKey:@"image"]];
        cell.userInteractionEnabled = NO;
        
        if (indexPath.row == _stateArr.count - 1) {
            //删除最后一个cell中的线
            [cell.line2 removeFromSuperview];
        }
        
        return cell;
    }else if (tableView.tag == 2){
        static NSString *identifier = @"cell1";
        MessageOrderTableViewCell *cell;
        
        cell = [[MessageOrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.userInteractionEnabled = NO;
        
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                
               // cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.titleLab.frame = CGRectMake(10, 10, WIDTH-20, 30);
                cell.titleLab.text = [NSString stringWithFormat:@"订单号码:%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"ordermum"]];
                
            }else if (indexPath.row == 2){
                
                cell.titleLab.text = [NSString stringWithFormat:@"包装费:￥%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"messtin_price"]];
                cell.moneyLab.text = [NSString stringWithFormat:@"配送费:￥%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"delivery_price"]];
                
            }else if (indexPath.row == 3){
                
                cell.titleLab.text = _dataArray2[3];
                cell.moneyLab.text = [NSString stringWithFormat:@"￥%@",_dataArray1[3]];
                
            }else if (indexPath.row == 4){
                
                cell.titleLab.text = _dataArray2[4];
                cell.moneyLab.text = [NSString stringWithFormat:@"￥%@",_dataArray1[4]];
                
            }else if (indexPath.row == 5){
                
                cell.titleLab.text = _dataArray2[5];
                cell.moneyLab.text = [NSString stringWithFormat:@"￥%@",_dataArray1[5]];
                
            }else if (indexPath.row == 6){
                
                cell.titleLab.text = _dataArray2[6];
                cell.moneyLab.text = [NSString stringWithFormat:@"￥%@",_dataArray1[6]];
                
            }else{
                
                cell.titleLab.frame = CGRectMake(10, 10, WIDTH - 100 * MYWIDTH, 30);
                cell.titleLab.textColor =UIColorFromRGB(0xe51c23);
                cell.titleLab.text = [NSString stringWithFormat:@"%@",[[_infoarr objectAtIndex:indexPath.row - 1] objectForKey:@"title"]];
                cell.moneyLab.text = [NSString stringWithFormat:@"￥%@",[[_infoarr objectAtIndex:indexPath.row - 1] objectForKey:@"subtotal"]];
                
                UILabel * labelNum =[[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2-30, 10, 60, 30)];
                labelNum.textAlignment =NSTextAlignmentCenter;
                labelNum.text =[NSString stringWithFormat:@"x %@",[[_infoarr objectAtIndex:indexPath.row - 1] objectForKey:@"num"]];
                labelNum.textColor =UIColorFromRGB(0xe51c23);
                labelNum.font =[UIFont systemFontOfSize:15];
                [cell addSubview:labelNum];
                
                cell.lineview.frame = CGRectMake(0, 50, WIDTH, 1);
                
            }
        }else if (indexPath.section == 2){
            if (indexPath.row == 0) {
                
                cell.userInteractionEnabled = YES;                      //cell 可点击
                cell.selectionStyle = UITableViewCellSelectionStyleNone;//cell 去掉点击变色
                cell.titleLab.text = @"期望时间：";
                cell.messageLab.text = [NSString stringWithFormat:@"%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"peisong"]];
//                cell.messageLab.backgroundColor =[UIColor redColor];
//                cell.titleLab.backgroundColor =[UIColor yellowColor];
                cell.messageLab.textColor =UIColorFromRGB(0xe51c23);
#pragma mark - 电话按钮
                UIButton * phoneBtn =[[UIButton alloc]initWithFrame:CGRectMake(WIDTH-100, 10, 80, 30)];
                [phoneBtn setTitle:@"电话确认" forState:UIControlStateNormal];
                phoneBtn.layer.cornerRadius = 5;
                phoneBtn.titleLabel.font =[UIFont systemFontOfSize:14];
                [phoneBtn setBackgroundColor:UIColorFromRGB(0x2ECC71)];
                [phoneBtn addTarget:self action:@selector(phonebt) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:phoneBtn];
                
            }else if (indexPath.row == 1){
                
                cell.titleLab.text = @"配送地址：";
                NSString *sexstr;
                if ([[_alldic objectForKey:@"addr"] count]>0) {
                    if ([[[_alldic objectForKey:@"addr"] objectForKey:@"sex"] intValue] == 0) {
                        sexstr = @" ";
                    }else if ([[[_alldic objectForKey:@"addr"] objectForKey:@"sex"] intValue] == 1){
                        sexstr = @"（先生）";
                    }else if ([[[_alldic objectForKey:@"addr"] objectForKey:@"sex"] intValue] == 2){
                        sexstr = @"（女士）";
                    }
                    
                    cell.messageLab.text = [NSString stringWithFormat:@"%@%@ %@",[[_alldic objectForKey:@"addr"] objectForKey:@"name"],sexstr,[[_alldic objectForKey:@"addr"] objectForKey:@"phone"]];
                    
                    cell.lineview.frame = CGRectMake(0, 84, WIDTH, 1);
                    
                    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80, 40, WIDTH - 90, 40)];
                    label.font = [UIFont systemFontOfSize:14];
                    label.numberOfLines = 2;
                    label.textAlignment = NSTextAlignmentLeft;
                    label.textColor = UIColorFromRGB(0x333333);
                    label.text = [NSString stringWithFormat:@"%@ %@",[[_alldic objectForKey:@"addr"] objectForKey:@"address"],[[_alldic objectForKey:@"addr"] objectForKey:@"full_address"]];
                    [cell addSubview:label];

                }
                
            }else if (indexPath.row == 2){
                
                cell.titleLab.text = @"配送服务：";
                cell.messageLab.text = [NSString stringWithFormat:@"%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"category_title"]];
                
            }
        }else if (indexPath.section == 3){
            if (indexPath.row == 0) {
                
                cell.titleLab.frame =CGRectMake(10, 10, WIDTH-20, 30);
                cell.titleLab.text = @"订单号码：";
                cell.messageLab.text = [NSString stringWithFormat:@"%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"ordermum"]];
                
            }else if (indexPath.row == 1){
                
                cell.titleLab.text = @"订单时间：";
                cell.messageLab.text = [NSString stringWithFormat:@"%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"create_time"]];
                
            }else if (indexPath.row == 2){
                
                cell.titleLab.text = @"支付方式：";
                cell.messageLab.text = [NSString stringWithFormat:@"%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"payment"]];
                
            }
        }else if (indexPath.section == 1){
            
//            cell.titleLab.text =[NSString stringWithFormat:@"总计：%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"price"]];
            
            cell.moneyLab.text = [NSString stringWithFormat:@"实付：￥%@",[[_alldic objectForKey:@"pseudo"] objectForKey:@"count_price"]];
            cell.messageLab.frame = CGRectMake(120, 10, WIDTH - 90, 30);
            cell.moneyLab.font = [UIFont systemFontOfSize:17];
            cell.titleLab.font = [UIFont systemFontOfSize:14];
            cell.titleLab.textColor = UIColorFromRGB(0x666666);
            
        }
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView.tag == 2) {
//        return 40;
        if (section == 1) {
            return 0;
        }else{
            return 40;
        }
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    view.backgroundColor = UIColorFromRGB(0xefeff1);
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 20)];
    lab.font = [UIFont systemFontOfSize:15];
    lab.textColor = UIColorFromRGB(0x333333);
    [view addSubview:lab];
    if (section == 0) {
        lab.text = @"订单详情";
    }else if (section == 2){
        lab.text = @"配送信息";
    }else if (section == 3){
        lab.text = @"订单信息";
    }
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
//    [[[_alldic objectForKey:@"pseudo"] objectForKey:@"status"] isEqualToString:@"2"]
    if ([[[_alldic objectForKey:@"pseudo"] objectForKey:@"status"] isEqualToString:@"1"])
    {
        if (tableView.tag == 2 && section == 3) {
            return 50 * MYHEIGHT;
        }
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (tableView.tag == 2 && section == 3) {
        
//        NSUserDefaults * user =[NSUserDefaults standardUserDefaults];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50 * MYHEIGHT)];
        view.backgroundColor = UIColorFromRGB(0xefeff1);
        //view.backgroundColor =[UIColor redColor];
        UIButton *cancelbtn = [[UIButton alloc]initWithFrame:CGRectMake((WIDTH-200*MYWIDTH)/2, 5, 200 * MYWIDTH, 40 * MYHEIGHT)];
        cancelbtn.backgroundColor = UIColorFromRGB(0x78909c);
        cancelbtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [cancelbtn setTitle:@"设为无效" forState:UIControlStateNormal];
        [cancelbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cancelbtn.layer.cornerRadius = 5;
        [cancelbtn addTarget:self action:@selector(cancelOrder) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:cancelbtn];
        //(WIDTH/2 - 150 * MYWIDTH, 5, 100 * MYWIDTH, 40 * MYHEIGHT)
//        UIButton *surebtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH/2 + 50 * MYWIDTH, 5, 100 * MYWIDTH, 40 * MYHEIGHT)];
//        surebtn.backgroundColor = UIColorFromRGB(0x9ccc65);
//        surebtn.titleLabel.font = [UIFont systemFontOfSize:17];
//        
//        [surebtn setTitle:[user objectForKey:@"state_order"] forState:UIControlStateNormal];
//        [surebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        surebtn.layer.cornerRadius = 5;
//        [surebtn addTarget:self action:@selector(sureOrder) forControlEvents:UIControlEventTouchUpInside];
//        [view addSubview:surebtn];
        
        return view;
        
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消被点击
}

#pragma mark 状态，详情转换

//订单状态
-(void)orderState:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        _orderStatebtn.userInteractionEnabled = NO;
        _orderMessagebtn.userInteractionEnabled = YES;
        _lineview.frame = CGRectMake(0, 49 * MYHEIGHT, WIDTH/2, 2);
        //_againOrderBtn.frame = CGRectMake(0, HEIGHT, WIDTH, 40);
        
        [UIView animateWithDuration:0.2 animations:^{
            [_orderStatebtn setTitleColor:UIColorFromRGB(MYColor) forState:UIControlStateSelected];
            _orderMessagebtn.selected= !btn.selected;
            _stateTab.frame = CGRectMake(0, 50 * MYHEIGHT, WIDTH, HEIGHT - 110 * MYHEIGHT);
            _messageTab.frame = CGRectMake(WIDTH, 50 * MYHEIGHT, WIDTH, HEIGHT - 110 * MYHEIGHT);
            //[_cancelOrderBtn setTitle:@"取消订单" forState:UIControlStateNormal];
            //_btnview.frame = CGRectMake(0, HEIGHT - 114, WIDTH, 50);
            //_againOrderBtn.frame = CGRectMake(0, HEIGHT, WIDTH, 50);
        }];
    }
    
    
}
//订单详情
-(void)orderMessage:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        _orderStatebtn.userInteractionEnabled = YES;
        _orderMessagebtn.userInteractionEnabled = NO;
        _lineview.frame = CGRectMake(WIDTH/2, 49 * MYHEIGHT, WIDTH/2, 2);
        //_btnview.frame = CGRectMake(0, HEIGHT, WIDTH, 40);
        [UIView animateWithDuration:0.2 animations:^{
            _orderStatebtn.selected= !btn.selected;
            [_orderMessagebtn setTitleColor:UIColorFromRGB(MYColor) forState:UIControlStateSelected];
            [_orderStatebtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
            _stateTab.frame = CGRectMake(-WIDTH,50 * MYHEIGHT, WIDTH, HEIGHT - 110 * MYHEIGHT);
            _messageTab.frame = CGRectMake(0, 50 * MYHEIGHT, WIDTH, HEIGHT - 120 * MYHEIGHT);
            //[_cancelOrderBtn setTitle:@"再来一单" forState:UIControlStateNormal];
            //_cancelOrderBtn.frame = CGRectMake(0, HEIGHT - 114, WIDTH, 50);
            //_againOrderBtn.frame = CGRectMake(0, HEIGHT - 114, WIDTH, 50);
        }];
    }
}

#pragma mark 确认订单或者取消订单
//
//-(void)orderbtn:(UIButton *)btn{
//    if (btn.tag == 1) {
//        [self cancelpay];
//    }else if (btn.tag == 2){
//        
//    }else if (btn.tag == 3){
//        //        ShoppingViewController *shopping = [[ShoppingViewController alloc]init];
//        //        [self.navigationController pushViewController:shopping animated:YES];
//    }
//}

//未接单.待派送.配送中.已完成
#pragma mark - 接单接单
-(void)sureOrder{
    NSUserDefaults * user =[NSUserDefaults standardUserDefaults];
//    NSString *url = [user objectForKey:@"state_url"];
    NSString *url =@"http://www.xsy777.com/app/b_send/jiedans.html";
    NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"],
                          @"id":_idStr};
//    NSLog(@"%@",dic);
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [manager POST:url parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            [self.view makeToast:@"已完成接单" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

//取消订单

-(void)cancelOrder{
    NSString *url = @"http://www.xsy777.com/app/demo/cancelorder.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{@"user_token":[user objectForKey:@"token"],
                           @"pseudo_id":_idStr};
//    NSLog(@"%@",dic1);
    AFHTTPRequestOperationManager *manager= [MyTools initAFHttpManager];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:url parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            //[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(getDate) userInfo:nil repeats:NO];
//            UIAlertView * alertview =[[UIAlertView alloc]initWithTitle:@"提示" message:@"请确认拒接此订单" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认拒接", nil];
//            [alertview show];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
            }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

#pragma mark 手势切换视图

-(void)handlePan:(UIPanGestureRecognizer *)sender{
    CGPoint point = [sender translationInView:self.view];
    if (sender.state == UIGestureRecognizerStateEnded) {
        if (point.x < 0) {
//            NSLog(@"向左滑动");
            if (_stateTab.frame.origin.x == 0) {
                [self orderMessage:_orderMessagebtn];
            }
        }else{
//            NSLog(@"向右滑动");
            if (_stateTab.frame.origin.x == -WIDTH) {
                [self orderState:_orderStatebtn];
            }
        }
    }
}

- (void)phonebt
{
    NSString * str =[NSString stringWithFormat:@"tel:%@",[[_alldic objectForKey:@"addr"] objectForKey:@"phone"]];
    NSLog(@"%@",str);
    
    UIWebView *callWebView = [[UIWebView alloc] init];
    
    NSURL *telURL = [NSURL URLWithString:str];//@"tel:10010"
    
    [callWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
    
    [self.view addSubview:callWebView];
//    UIAlertView * alertView =[[UIAlertView alloc]initWithTitle:@"请确认拨打电话" message:[[_alldic objectForKey:@"addr"] objectForKey:@"phone"] delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
//    [alertView show];
    
}

//-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if (buttonIndex == 0) {
//        NSString * str =[NSString stringWithFormat:@"tel:%@",[[_alldic objectForKey:@"addr"] objectForKey:@"phone"]];
//        NSLog(@"%@",str);
//        
//        UIWebView *callWebView = [[UIWebView alloc] init];
//        
//        NSURL *telURL = [NSURL URLWithString:@"tel:10010"];
//        
//        [callWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
//        
//        [self.view addSubview:callWebView];
//    }
//}


-(void)back{
    _stateTab.frame = CGRectMake(0, 50, WIDTH, HEIGHT - 110);
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    [self getDate];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
